home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / sw / ship.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.4 KB  |  239 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #ifndef SHIP_H
  18. #define    SHIP_H
  19.  
  20. #include "comm.h"
  21. #include <Inventor/SbLinear.h>
  22. class SoSeparator;
  23. class SoTransform;
  24. class SoTranslation;
  25. class SoRotation;
  26. class SoSwitch;
  27. class SoMaterial;
  28. class SoCoordinate3;
  29.  
  30. #define    TURRETRADIUS    1.0            // 1 meter
  31. #define    BEAMRADIUS    0.2            // 20 cm
  32.  
  33. struct Ray {                    // 3D ray
  34.   public:
  35.     SbVec3f        o;            // origin of ray
  36.     SbVec3f        d;            // direction of ray
  37. };
  38.  
  39. class ShipObject {
  40.   public:
  41.     enum Shield        { FrontShield = 0, RearShield = 1,
  42.               LeftShield = 2, RightShield = 3,
  43.               TopShield = 4, BottomShield = 5 };
  44.  
  45.             ShipObject(NetId, Team, const char* callSign);
  46.     virtual        ~ShipObject();
  47.  
  48.     Team        team() const;        // return my team
  49.     char*        name() const;        // return my name
  50.     int            score() const;        // my score
  51.     int            won() const;        // number of wins
  52.     int            lost() const;        // number of losses
  53.     Team        flag() const;        // which flag I have
  54.     void        flag(Team);        // set which flag I have
  55.     virtual ShipClass    shipClass() const = 0;    // ship's class id
  56.     NetId        id() const;        // host id of ship
  57.  
  58.     SoSeparator*    root() const;        // return root of ship scene
  59.     void        reset();        // reset ship
  60.     void        advance(float dt);    // step stuff in time (dt sec)
  61.  
  62.     // general ship info
  63.     ShipInfo&        shipInfo();        // get ship information
  64.     virtual float    mass() const;        // mass of ship (kg)
  65.     int            exploding() const;    // TRUE if blowing up
  66.     float        explodeTime() const;    // fraction completed
  67.     Active        active() const;        // return activity state
  68.     void        active(Active);        // set active state
  69.     int            hittable() const;    // active and not exploding
  70.     float        shipBound() const;    // radius of bounding sphere
  71.     int            shipVisibility() const;    // return ship visibility
  72.     void        shipVisibility(int);    // set ship visibilty
  73.  
  74.     // get position, orientation, and velocities
  75.     SbVec3f        position() const;    // get ship position
  76.     const SbRotation&    orientation() const;    // get ship orientation
  77.     const SbVec3f    direction() const;    // get ship direction
  78.     SbVec3f        velocity() const;    // get ship velocity
  79.     virtual float    angularAccel() const;    // ang. acceleration (rad/s/s)
  80.  
  81.     // set position, orientation, and velocities
  82.     void        position(const SbVec3f&);
  83.     void        orientation(const SbRotation&);
  84.     void        velocity(const SbVec3f&);
  85.     void        targetAngVel(const SbVec3f&);
  86.  
  87.     // engines
  88.     void        engineOutput(float);    // fraction max output (0-1)
  89.     float        engineOutput() const;    // fraction max output (0-1)
  90.     float        engineForce() const;    // force from engines (N)
  91.     virtual float    engineMaxForce() const;    // maximum engine force (N)
  92.     virtual float    engineResponse() const;    // max output change per second
  93.     float        fuelLeft() const;    // remaining fuel (frac max)
  94.     void        fuelLeft(float);    // set remaining fuel
  95.     virtual float    fuelRate() const;    // fuel/sec at max thrust
  96.  
  97.     // shields
  98.     float        shield(Shield) const;    // energy protection (J)
  99.     virtual float    shieldMaximum() const;    // maximum shield strength (J)
  100.     virtual float    shieldRecovery() const;    // protection recovery (J/s)
  101.     void        shieldStrength(float);    // set shield strength
  102.  
  103.     // weapons
  104.     int            fireMissile(ObjectInfo*);    // return TRUE if fired
  105.     int            numMissiles() const;    // missiles left on board
  106.     void        numMissiles(int);    // set number of missiles
  107.     virtual int        maxMissiles() const;    // max missiles ship holds
  108.     ObjectInfo&        missileInfo(int);    // get missile info
  109.     virtual float    missileEnergy() const;    // energy from explosion (J)
  110.     float        missileRadius() const;    // radius of detonation
  111.     float        missileBound() const;    // bounding sphere radius
  112.     int            missileHittable(int) const;    // active, !exploding
  113.     int            missileVisibility(int n) const;    // return visiblity
  114.     void        missileVisibility(int n, int);    // set missile visiblity
  115.     int            missileReady() const;    // TRUE if can fire
  116.  
  117.     int            fireLaser();        // return TRUE if fired
  118.     float        laserHeat() const;    // laser overheat (frac max)
  119.     virtual float    laserPower() const;    // laser power (J/s)
  120.     SbVec3f        laserPosition() const;    // laser position (world)
  121.     SbVec3f        laserDirection() const;    // laser direction (world)
  122.     const SbVec3f&    turretPosition() const;    // laser position (local)
  123.     const SbVec3f&    trackDirection() const;    // laser direction (local)
  124.     void        trackDirection(const SbVec3f&);    // set target direction
  125.  
  126.     // hit detection
  127.     float        laserHitShip(const Ray&) const;
  128.     float        laserHitMissile(const Ray&, int n) const;
  129.     float        laserHitAsteroid(const Ray&, int a) const;
  130.     float        missileHitShip(const Ray&, float radius) const;
  131.     float        missileHitMissile(const Ray&, float rad, int n) const;
  132.     float        missileHitAsteroid(const Ray&, float rad, int a) const;
  133.  
  134.     // damage
  135.     void        hitShield(const SwHitMessage&);
  136.     void        explodeShip(InAddr killer);
  137.     void        explodeMissile(const SwHitMessage&);
  138.  
  139.     // flags
  140.     void        dropFlag();
  141.     void        grabFlag(Team);
  142.  
  143.     // communication
  144.     void        read(const ShipInfo&);    // set me according to info
  145.  
  146.     // utility functions
  147.     void        findWorldDirection(const SbVec3f&, SbVec3f&) const;
  148.     void        findWorldPosition(const SbVec3f&, SbVec3f&) const;
  149.     void        findLocalDirection(const SbVec3f&, SbVec3f&) const;
  150.     void        findLocalPosition(const SbVec3f&, SbVec3f&) const;
  151.     void        findLocalPosition(float p[3], SbVec3f&) const;
  152.  
  153.   protected:
  154.     void        init(SoSeparator* shipGeometry,
  155.                 SoSeparator* shipChangingGeometry,
  156.                 const SbVec3f& laserTurretPosition,
  157.                 const SbVec3f& flagPosition,
  158.                 const SbVec3f& cockpitPosition);
  159.     virtual void    advanceExtra(float dt);    // advance ship specific stuff
  160.     virtual void    newEngineOutput() = 0;    // change engine geometry
  161.     virtual SbVec3f    missileSource(int) const = 0;    // missile start pos
  162.     virtual int        missileSilos() const = 0;    // number missile silos
  163.  
  164.   private:
  165.     SoSeparator*    makeLaserTurret();
  166.     SoSeparator*    makeLaserBeam();
  167.     SoSeparator*    makeShields();
  168.  
  169.     void        advanceCamera();
  170.     void        advancePosition(float dt);
  171.     void        advanceOrientation(float dt);
  172.     void        advanceEngine(float dt);
  173.     void        advanceShield(float dt);
  174.     void        advanceMissile(int, float dt);
  175.     void        advanceLaser(float dt);
  176.     void        advanceTurn(const SbVec3f&, SbVec3f&,
  177.                     float maxAcc, float dt) const;
  178.     Shield        shieldNumber(const SbVec3f& i) const;
  179.     float        dissipate(float d) const;
  180.  
  181.   private:
  182.     Team        myTeam;            // which team I'm on
  183.     char*        myName;            // my call sign
  184.     ShipInfo        info;
  185.     SbVec3f        cockpit;        // cockpit position
  186.     SbRotation        rotation;        // ship orientation
  187.     SbMatrix        rotateMatrix;        // rotation matrix
  188.     SbVec3f        angularVelDir;        // angular velocity
  189.     SbVec3f        angularVelDirTarget;    // target angularVelDir
  190.     SbVec3f        trackingDirection;    // direction laser is pointing
  191.     SbVec3f        trackingDirTarget;    // target trackingDirection
  192.     SbVec3f        flagPosition;        // position of flags
  193.     float        engineOutputTarget;    // target for engineOutput
  194.     float        fuel;            // remaining fuel
  195.     ObjectInfo*        missileTarget[MAXMISSILES];    // missile targets
  196.     int            missilesLeft;        // remaining missiles
  197.     float        missileTime[MAXMISSILES];
  198.     float        laserTime;        // time to show laser beam for
  199.     float        laserTemp;        // >1 is overheated
  200.     float        sx, sy, sz;        // ship center
  201.     float        sdx, sdy, sdz;        // ship size
  202.     float        sbs;            // ship bounding sphere radius
  203.     float        mdx, mdy, mdz;        // missile size
  204.     float        mbs;            // missile bounding radius
  205.     int            shipVisible,
  206.             missileVisible[MAXMISSILES];
  207.     int            nextSilo;        // next silo to shoot from
  208.     int            cantGrabFlag[NUMTEAMS];    // prevent immediate re-grab
  209.  
  210.     SoSeparator*    wholeShip;        // root node of all ship stuff
  211.     SoSwitch*        activeSwitch;        // show/hide all ship stuff
  212.     SoSwitch*        shipSwitch;        // show/hide ship
  213.     SoTranslation*    shipPosition;        // ship position node
  214.     SoRotation*        shipOrientation;    // ship orientation node
  215.     SoSwitch*        missileSwitch[MAXMISSILES];    // show/hide missile
  216.     SoTranslation*    missilePosition[MAXMISSILES];
  217.     SoRotation*        missileOrientation[MAXMISSILES];
  218.     SoSwitch*        laserSwitch;        // laser beam on/off switch
  219.     SoTranslation*    laserLocation;        // laser translation node
  220.     SoRotation*        laserOrientation;    // laser rotation node
  221.     SoSwitch*        hitSwitch[MAXHITS];    // turn shield hits on/off
  222.     SoMaterial*        hitMaterial[MAXHITS];    // hit shading
  223.     SoTransform*    hitTransform[MAXHITS];    // location of hit on shield
  224.     SoCoordinate3*    beamCoord;        // laser beam geometry
  225.     SoSwitch*        flagSwitch;        // choose which flag visible
  226.  
  227.     float        laserBeamPoints[10][3];    // vertices for laser beam
  228.  
  229.     static SoSeparator*    missileGeom;        // missile object
  230. };
  231.  
  232. // Once a ship is added to the array or missile added to ship's missile array
  233. // it must not move from that position in the list.  Otherwise the
  234. // missileTarget entry will start pointing to the wrong thing.
  235.  
  236. // reset() *must* be called after construction.
  237.  
  238. #endif
  239.